from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.scrollview import ScrollView
from kivy.core.window import Window
from kivy.utils import get_color_from_hex


import os

database_url = os.getenv('DATABASE_URL')
secret_key = os.getenv('SECRET_KEY')

if database_url:
    print(f"Connecting to database...")
else:
    print("DATABASE_URL not set!")

class MultiplicationTableApp(App):
    def build(self):
        # 设置窗口背景色为浅蓝色
        Window.clearcolor = get_color_from_hex('#e6f7ff')

        # 创建主布局
        main_layout = GridLayout(cols=1, spacing=10, padding=10)

        # 添加标题
        title = Label(
            text='9x9乘法表',
            font_size='24sp',
            size_hint_y=None,
            height=50,
            color=get_color_from_hex('#003366')
        )
        main_layout.add_widget(title)

        # 创建滚动视图
        scroll_view = ScrollView(size_hint=(1, 1))

        # 创建9x9网格布局
        grid_layout = GridLayout(cols=10, spacing=5, size_hint_y=None)
        grid_layout.bind(minimum_height=grid_layout.setter('height'))

        # 添加表头
        grid_layout.add_widget(Label(text='', size_hint_y=None, height=40))
        for i in range(1, 10):
            header = Label(
                text=str(i),
                font_size='18sp',
                size_hint_y=None,
                height=40,
                color=get_color_from_hex('#003366')
            )
            grid_layout.add_widget(header)

        # 添加乘法表内容
        for i in range(1, 10):
            # 添加行号
            row_header = Label(
                text=str(i),
                font_size='18sp',
                size_hint_y=None,
                height=40,
                color=get_color_from_hex('#003366')
            )
            grid_layout.add_widget(row_header)

            # 添加乘法结果
            for j in range(1, 10):
                result = i * j
                btn = Button(
                    text=str(result),
                    font_size='16sp',
                    size_hint_y=None,
                    height=40,
                    background_color=get_color_from_hex('#4d94ff') if (i + j) % 2 == 0 else get_color_from_hex(
                        '#99ccff')
                )
                grid_layout.add_widget(btn)

        # 将网格布局添加到滚动视图
        scroll_view.add_widget(grid_layout)

        # 将滚动视图添加到主布局
        main_layout.add_widget(scroll_view)

        return main_layout


if __name__ == '__main__':
    MultiplicationTableApp().run()